{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "# input_or _print\n",
    "\n",
    "给大家带来解析输入输出的用法"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "<font size=3>\n",
    "  \n",
    "白话内容查看：[input_or _print](https://github.com/jhcgt4869/practice_Paddle/blob/main/python%E5%AE%9E%E8%B7%B5/print_or_input/print_or_input.md)  \n",
    " 输入输出实战：[input_or _print](https://github.com/jhcgt4869/practice_Paddle/blob/main/python%E5%AE%9E%E8%B7%B5/print_or_input/print_or_input.py)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "## input()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入你的名字"
     ]
    },
    {
     "data": {
      "text/plain": [
       "'xxx'"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "input('请输入你的名字')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入你的名字"
     ]
    }
   ],
   "source": [
    "name = input('请输入你的名字')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "## print()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "xxx\n"
     ]
    }
   ],
   "source": [
    "print(name)  # 不添加引号是因为输出的是变量名而不是字符串"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "name\n"
     ]
    }
   ],
   "source": [
    "print('name')  # 添加引号输出的是字符串"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "单引号和双引号不可以换行\n",
      "三引号可以换行\n",
      "\n"
     ]
    }
   ],
   "source": [
    "# 输出可以换行的数据\r\n",
    "print('''\r\n",
    "单引号和双引号不可以换行\r\n",
    "三引号可以换行\r\n",
    "''')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "### 输出不同数据类型的拼接"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "# 给变量进行赋值\r\n",
    "height = 180\r\n",
    "age = 20"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "<font size=3>\n",
    "  \n",
    "  字符拼接"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "print('名字：' + name + '身高:' + height + '年龄:' + age)  # 使用+只能够拼接字符串，不能够拼接数字"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "由于只有字符串才可以进行拼接所以会有报错\n",
    "![](https://ai-studio-static-online.cdn.bcebos.com/14b589376a7243bb96dba0a5a81165dc0f65d8fc9c334e4b989daea20aded023)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "名字：xxx身高:180年龄:20\n"
     ]
    }
   ],
   "source": [
    "print('名字：' + name + '身高:' + str(height) + '年龄:' + str(age))  # 数字类型要强制转换"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Hello World Hello World Hello World \n"
     ]
    }
   ],
   "source": [
    "print('Hello World ' * 3)  # 使用*进行复制输出"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "<font size=3>\n",
    "  \n",
    "换行修改\n",
    "  ___\n",
    "  使用`, end=''`进行修改"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "不修改换行\n",
      "0\n",
      "1\n",
      "2\n",
      "3\n",
      "4\n",
      "修改换行\n",
      "0 1 2 3 4 "
     ]
    }
   ],
   "source": [
    "# 此处先超纲使用for循环给大家演示（只要知道怎么样换行就行，不需要理解for循环）\r\n",
    "print('不修改换行')\r\n",
    "for i in range(5):  # 循环0-4\r\n",
    "    print(i)  # 换行输出\r\n",
    "print('修改换行')\r\n",
    "for i in range(5):  \r\n",
    "    print(i, end=' ')  # 使用空格代替换行"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "在引号中输入需要替换的就好了，可以是任意的东西哦~~~"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "### %占位符"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "\n",
    "\n",
    "| 符   号 | 描述                                 |\n",
    "| :-----: | ------------------------------------ |\n",
    "|   %c    | 格式化字符及其ASCII码                |\n",
    "|   %s    | 格式化字符串                         |\n",
    "|   %d    | 格式化整数                           |\n",
    "|   %u    | 格式化无符号整型                     |\n",
    "|   %o    | 格式化无符号八进制数                 |\n",
    "|   %x    | 格式化无符号十六进制数               |\n",
    "|   %X    | 格式化无符号十六进制数（大写）       |\n",
    "|   %f    | 格式化浮点数字，可指定小数点后的精度 |\n",
    "|   %e    | 用科学计数法格式化浮点数             |\n",
    "|   %E    | 作用同%e，用科学计数法格式化浮点数   |\n",
    "|   %g    | %f和%e的简写                         |\n",
    "|   %G    | %f 和 %E 的简写                      |\n",
    "|   %p    | 用十六进制数格式化变量的地址         |\n",
    "\n",
    "----\n",
    "只要对照上表数据类型正确即可！"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "名字:xxx\n",
      "身高:180\n",
      "年龄:20\n"
     ]
    }
   ],
   "source": [
    "# 单个数据输出\r\n",
    "print('名字:%s' % name)\r\n",
    "print('身高:%d' % height) \r\n",
    "print('年龄:%d' % age)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "名字：xxx身高:180年龄:20\n"
     ]
    }
   ],
   "source": [
    "# 多个数据输出\r\n",
    "print('名字：%s身高:%d年龄:%d' % (name, height, age)) "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "名字：xxx身高:180.000000年龄:20\n"
     ]
    }
   ],
   "source": [
    "print('名字：%s身高:%f年龄:%d' % (name, height, age))   # 如果使用浮点型展示整型会\"强制转换\"成浮点型（小数）"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "浮点型数据类型的不同展示方法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "180.000000\n",
      "180.00000000\n",
      "180.00\n",
      "180\n",
      " 180.00\n",
      "         180\n"
     ]
    }
   ],
   "source": [
    "print('%f' % height)\r\n",
    "print('%.8f' % height)\r\n",
    "print('%.2f' % height)\r\n",
    "print('%.f' % height)\r\n",
    "print('%7.2f' % height)\r\n",
    "print('%12.f' % height)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "<font size=3>\n",
    "  \n",
    "  说明  \n",
    "  ***  \n",
    "  %f中间添加`.`以后就分为小数点前和小数点后  \n",
    "  小数点前的表示整个数字的宽度  \n",
    "  如果大于实际宽度（例如上面例子的最后两个）  \n",
    "  就在左边添加空格  \n",
    "  如果小于实际宽度（例如上面例子的前4个）  \n",
    "  就以实际宽度为准  \n",
    "  ***    \n",
    "  小数点后面的数据就是小数的长度  \n",
    "  没有就默认6位  \n",
    "  有就按照设置的长度走\n",
    " "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "## format()\n",
    "***  \n",
    "此处就说几个基本使用，其他的会在后面的专题中说明"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "名字:xxx\n",
      "身高:180\n",
      "年龄:20\n",
      "名字:xxx身高:180年龄:20\n"
     ]
    }
   ],
   "source": [
    "# 单个和多个的用法\r\n",
    "print('名字:{}'.format(name))\r\n",
    "print('身高:{}'.format(height)) \r\n",
    "print('年龄:{}'.format(age))\r\n",
    "print('名字:{}身高:{}年龄:{}'.format(name, height, age)) "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "名字:xxx\n",
      "身高:180\n",
      "年龄:20\n",
      "名字:xxx身高:180年龄:20\n"
     ]
    }
   ],
   "source": [
    "# 缩写的用法\r\n",
    "print(f'名字:{name}')\r\n",
    "print(f'身高:{height}') \r\n",
    "print(f'年龄:{age}')\r\n",
    "print(f'名字:{name}身高:{height}年龄:{age}') "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "## 结束语\n",
    "这里是三岁！  \n",
    "这一次的输入输出就到这里啦  \n",
    "如果有问题几个给我提issue！  \n",
    "GitHub：[https://github.com/jhcgt4869/practice_Paddle/issues](https://github.com/jhcgt4869/practice_Paddle/issues)  \n",
    "gitee：[https://gitee.com/jhcgt4869/practice_Paddle/issues](https://gitee.com/jhcgt4869/practice_Paddle/issues)  \n",
    "会及时回复大家的问题！"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "PaddlePaddle 2.1.0 (Python 3.5)",
   "language": "python",
   "name": "py35-paddle1.2.0"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
